"""
This file is part of GSBEdit. For copyright and licensing information, please
see gsbedit.py in the GSBEdit base directory.
"""


import wx

import app.module_config as mc


MCT_CLASS_TREE = 4000


class ModuleClassTree(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        
        self.parent = parent
        self.current_class = None
        
        self.configs = mc.ModuleConfig('data/modules.cfg')
        
        self.class_tree = wx.TreeCtrl(self, MCT_CLASS_TREE, style = wx.TR_HAS_BUTTONS | wx.TR_LINES_AT_ROOT | wx.BORDER_SUNKEN)
        
        self.content = wx.BoxSizer(wx.VERTICAL)
        self.content.Add(self.class_tree, 1, wx.EXPAND)
        self.SetSizer(self.content)
        
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.select_class, id=MCT_CLASS_TREE)
        
        self.refresh_tree()
    

    def refresh_tree(self):
        root = self.configs.get_config('module')
        root_object = self.class_tree.AddRoot('Module', data=wx.TreeItemData('custom'))
        
        for subtype in root['subtypes']:
            self.add_class_tree(root_object, subtype, [subtype])
        
        self.class_tree.Expand(root_object)
        self.class_tree.SelectItem(root_object)
        
    
    def add_class_tree(self, root, type, full_path):
        joined_path = '.'.join(full_path)
        this_class = self.configs.get_config(joined_path)
        guiname = this_class['guiname']
        this_class_object = self.class_tree.AppendItem(root, guiname, data=wx.TreeItemData(joined_path))
        
        for subtype in this_class['subtypes']:
            self.add_class_tree(this_class_object, subtype, full_path + [subtype])
        

    def select_class(self, event):
        self.current_class = self.class_tree.GetItemData(event.GetItem()).GetData()
    
    
    def get_selected_class(self):
        return self.current_class
